#include "C_SQLiteSpaceRegion.h"


C_SQLiteSpaceRegion::C_SQLiteSpaceRegion(std::string path)
{
	m_sql = new C_SQLite();
	C_SQLiteTable tbl(m_sql);
	tbl.setName("space_regions");
	tbl.add("x INTEGER NOT NULL");
	tbl.add("y INTEGER NOT NULL");

	m_sql->createIfNotExist(path, &tbl);
}


C_SQLiteSpaceRegion::~C_SQLiteSpaceRegion()
{
}

void C_SQLiteSpaceRegion::add(int x, int y)
{
	C_SQLiteInsert in(m_sql);
	in.toTable("space_regions");
	in.add("x", std::to_string(x));
	in.add("y", std::to_string(y));
	in.exec();
}

bool C_SQLiteSpaceRegion::find(int x, int y)
{
	C_SQLiteSelect sel(m_sql);
	sel.setTable("space_regions");
	sel.setColumns("x,y");
	sel.setWhere("x='" + std::to_string(x) + "' AND y='" + std::to_string(y) + "'");
	sqlite3_stmt *statement;
	m_sql->execStmt(sel.execToStr(), statement);

	bool result = false;
	int arg = sqlite3_step(statement);
	if (arg == SQLITE_ROW)
	{
		result = true;
	}

	this->add(x, y);
	return result;
}
